package io.oschina.binv.kapp

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import io.oschina.binv.kapp.framework.BaseActivity
import io.oschina.binv.kapp.framework.MediaPickerFragment
import io.oschina.binv.kapp.framework.ReuseActivity
import io.oschina.binv.kapp.framework.RxBus
import io.oschina.binv.kapp.framework.widget.MediaItem
import io.oschina.binv.kapp.framework.widget.SelectItemsEvent
import io.reactivex.functions.Consumer

class LoginActivity : BaseActivity() {

    init {
        //必须放置此处,否则因为用户操作,或androi生命周期原因会多次执行,导致内存泄露(DESTROY后,才会销毁)
        RxBus.toObservable(SelectItemsEvent::class.java)
                .compose(ioToMain())
                .subscribe(Consumer {
                    data->
                    Log.i(data.selectItems.size.toString())
                },Consumer {
                    t->t.printStackTrace()
                })
    }

    override fun setContentView() {
        setContentView(R.layout.activity_login)
    }

    override fun initViewsAndLocalData(savedIntent: Intent): (outIntent: Intent) -> Unit {
        findViewById<Button>(R.id.button).setOnClickListener { _->
            startActivity(Intent(this@LoginActivity, MainActivity::class.java))
            finish()
        }

        findViewById<Button>(R.id.button2).setOnClickListener { _->
            val intent=Intent(this@LoginActivity, ReuseActivity::class.java)
            intent.putExtra(ReuseActivity.FRAGMENT_NAME, MediaPickerFragment::class.java.canonicalName)
            intent.putExtra(ReuseActivity.FRAGMENT_ARGS, Bundle().apply {
                putBoolean(MediaPickerFragment.PAYLOAD_HASE_CAMERA,false)
                putParcelableArrayList(MediaPickerFragment.PAYLOAD_SELECT_ITEMS, arrayListOf<MediaItem>() )
                putInt(MediaPickerFragment.PAYLOAD_SELECT_LIMIT, 9 )
            })
            startActivity(intent)
        }
        return {}
    }

    override fun loadRemoteData() {

    }
}
